<?php namespace Member\Controller;

use Hdphp\Controller\Controller;
use Member\Model\Index;

class IndexController extends Controller{
	protected $db;
	//构造函数
	public function __init()
	{


		if (!isset($_SESSION['user_id'])) {
			$_SESSION['module'] = MODULE;
			$_SESSION['controller'] = CONTROLLER;
			$_SESSION['action'] = ACTION;
			go('Member/User/login');

		}

		$this->db = new Index;

	}



	public function myInfo()
	{

		if(IS_POST)
		{
			if($this->db->myInfo()){

				View::ajax(array('code'=>0,'message'=>'修改成功'));
			}
			else
			{
				View::ajax(array('code'=>0,'message'=>$this->db->getError()));
			}
		}
		else
		{
			$data = $this->db->getAll();
			View::with('data',$data);
			View::make('myInfo.html');
		}



	}

	public function order(){

		$orders = Db::table('shop_order')->where('user_id')->get();

		foreach ($orders as $k => $v) {
			$order = Db::table('shop_order_list ol')
			->join('shop_goods g','ol.goods_id','=','g.goods_id')
			->join('shop_pics p','p.goods_id','=','g.goods_id')
			->where('ol.order_id',$v['order_id'])
			->get();
			$orders[$k]['goodlist']=$order;
		}
		// p($orders);
		View::with('orders',$orders);


		View::make('order.html');
	}

	public function focus(){

		$focus = Db::table('shop_focus h')
		->join('shop_goods g','g.goods_id','=','h.goods_id')
		->join('shop_pics p','p.goods_id','=','h.goods_id')
		->orderBy('h.create_at')->get();


		View::with('focus',$focus);
		View::make('focus.html');


	}


	public function myAddress()
	{
		$model = new \Member\Model\MyAddress;

		if(IS_POST)
		{
			if($model->myAddress()){

				View::ajax(array('code'=>0,'message'=>'操作成功'));
			}
			else
			{
				View::ajax(array('code'=>1,'message'=>$model->getError()));
			}
		}
		else
		{
			$data = $model->getAll();
			View::with('data',$data);

			View::make('myaddress.html');
		}

	}


	public function editAddress()
	{
		// p($_GET);exit;
		$field = Db::table('shop_address')->where('address_id',Q('get.address_id',0))->first();
		View::ajax($field);
	}

	public function delAddress()
	{
		$model = new \Member\Model\MyAddress;
		if($model->delAddress()){

			View::ajax(array('code'=>0,'message'=>'删除成功'));
		}
		else
		{
			View::ajax(array('code'=>1,'message'=>'删除失败'));
		}
	}


	public function balance()
	{
		$goods = $this->getOrderData();

		$address = Db::table('shop_address')->where('user_id',$_SESSION['user_id'])->where('defaultadd',1)->first();
		// p($goods);
		View::with('address',$address);
		View::with('goods',$goods);
		View::make('balance.html');
	}



	public function getOrderData()
	{
		$goods = array();
		foreach ($_SESSION['shopcar'] as $a => $b) {
			$pic = Db::table('shop_pics')->where('goods_id',$b['goods_id'])->pluck('small_pic');
			$good = Db::table('shop_goods')->where('goods_id',$b['goods_id'])->first();
			$shopstore = Db::table('shop_store')->where('mix_id',$b['mix_id'])->first();
			$attrs = array();
			$mix_id = $b['mix_id'];
			$prices = $good['shop_price'];
			foreach (explode("_",$b['mix_id']) as $c => $d) {
				$attr = Db::table('shop_goods_attr ga')
				->join('shop_attr a','ga.attr_id','=','a.attr_id')
				->where('ga.good_attr_id',$d)
				->first();

				$attrs[]=$attr;
				$prices+=$attr['add_price'];
			}
			$goods[$a]['pic']=$pic;
			$goods[$a]['good']=$good;
			$goods[$a]['shopstore']=$shopstore;
			$goods[$a]['attrs']=$attrs;
			$goods[$a]['mix_id']=$mix_id;
			$goods[$a]['prices']=$prices;
			$goods[$a]['buynum']=$b['buynum'];
		}


		return $goods;

	}
	public function balanceSuccess(){
		$good = $this->getOrderData();
		$order = array();
		$o = array();
		$o['order_num'] = 0;
		$o['total_price'] = 0;
		$address = Db::table('shop_address')->where('address_id',Q('get.address_id'))->first();
		foreach ($good as $key => $value) {
			$o['order_num']+=$value['buynum'];
			$o['total_price']+=$value['prices']*$value['buynum']*1;
		}
		$o['consignee']=$address['consignee'];
		$o['rec_address']=$address['province'].$address['city'].$address['county'].$address['detailaddress'];
		$o['create_at']=time();
		$o['buy_order_num']=time();
		$o['user_id']=$_SESSION['user_id'];

		Db::table('shop_order')->insert($o);

		$order_id = Db::getInsertId();

		foreach ($good as $k => $v) {
			$order['buy_num']=$v['buynum'];
			$order['all_price']=$v['prices']*$v['buynum']*1;
			$order['goods_id']=$v['good']['goods_id'];
			$order['mix_id']=$v['mix_id'];
			$order['order_id']=$order_id;
			Db::table('shop_order_list')->insert($order);
		}

		View::make('balancesuccess.html');
	}


	public function scanHistory(){
		$history = Db::table('shop_history h')
		->join('shop_goods g','g.goods_id','=','h.goods_id')
		->join('shop_pics p','p.goods_id','=','h.goods_id')
		->orderBy('h.create_at')->get();

		View::with('history',$history);
		View::make('scanhistory.html');
	}






}
